package com.lz.frame.utils;


import com.lz.frame.config.security.MyUserDetails02;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityUtil implements SecurityUtilImpl {

    public static MyUserDetails02 getCurrentUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context != null) {
            return (MyUserDetails02) context.getAuthentication().getPrincipal();
        }
        return null;
    }

    @Override
    public String getId() {
        try {
            SecurityContext context = SecurityContextHolder.getContext();
            if (context != null && context.getAuthentication() != null && context.getAuthentication().isAuthenticated()) {
                Object principal = context.getAuthentication().getPrincipal();
                if (principal instanceof MyUserDetails02) {
                    return ((MyUserDetails02) principal).getUserVO().getUser().getId();
                }
            }
            return "1";
        } catch (Exception e) {
            // 可选日志记录
            return null;
        }
    }

}